﻿using System;
using System.Collections.Generic;

namespace Murry.DataStore
{
    /// <summary>
    /// DataScheme collection.
    /// </summary>
    public class DataSchemes : IEnumerable<DataScheme>
    {
        /// <summary>
        /// Schemes are kept internally in a dictionary.
        /// </summary>
        protected Dictionary<string, DataScheme> Schemes = new Dictionary<string, DataScheme>();

        /// <summary>
        /// Add a new scheme.
        /// </summary>
        /// <param name="scheme">Scheme</param>
        public void Add(DataScheme scheme)
        {
            if (String.IsNullOrEmpty(scheme.Name))
            {
                throw new ArgumentException("Scheme Name must not be null.");
            }

            Schemes[scheme.Name] = scheme;
        }

        /// <summary>
        /// Retrieve scheme by name.
        /// </summary>
        /// <param name="name">Name</param>
        /// <returns>Scheme.</returns>
        public DataScheme this[string name]
        {
            get
            {
                if (String.IsNullOrEmpty(name))
                {
                    throw new ArgumentException("Scheme Name must not be null.");
                }

                if (!Schemes.ContainsKey(name))
                {
                    throw new ArgumentException("Scheme not bound by that name.");
                }

                return Schemes[name];
            }
        }

        /// <summary>
        /// Allow iteration through the schemes.
        /// </summary>
        /// <returns>Enumerator</returns>
        public IEnumerator<DataScheme> GetEnumerator()
        {
            return Schemes.Values.GetEnumerator();
        }

        /// <summary>
        /// Allow iteration through the schemes.
        /// </summary>
        /// <returns>Enumerator</returns>
        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }
    }
}
